﻿@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore

@inherits DbContextPage

@if (orders == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6">
                <RadzenDataGrid ColumnWidth="200px" AllowFiltering="true" AllowPaging="true" PageSize="5" AllowSorting="true" Data="@orders" TItem="Order" @bind-Value="@SelectedOrders">
                    <Columns>
                        <RadzenDataGridColumn TItem="Order" Property="OrderID" Title="Order ID" Width="120px" />
                        <RadzenDataGridColumn TItem="Order" Property="Customer.CompanyName" Title="Customer" Width="200px" />
                        <RadzenDataGridColumn TItem="Order" Property="Employee.LastName" Title="Employee" Width="200px" >
                            <Template Context="order">
                                <RadzenImage Path="@order.Employee?.Photo" style="width: 32px; height: 32px; border-radius: 16px; margin-right: 6px;" />
                                @order.Employee?.FirstName @order.Employee?.LastName
                            </Template>
                        </RadzenDataGridColumn>
                        <RadzenDataGridColumn TItem="Order" Property="OrderDate" Title="Order Date" FormatString="{0:d}" Width="140px" />
                        <RadzenDataGridColumn TItem="Order" Property="RequiredDate" Title="Required Date" FormatString="{0:d}" Width="140px" />
                        <RadzenDataGridColumn TItem="Order" Property="ShippedDate" Title="Shipped Date" FormatString="{0:d}" Width="140px" />
                        <RadzenDataGridColumn TItem="Order" Property="ShipName" Title="Ship Name" />
                        <RadzenDataGridColumn TItem="Order" Property="ShipCountry" Title="Ship Country" />
                    </Columns>
                </RadzenDataGrid>
            </div>
            <div class="col-md-6">
                <RadzenCard Style="margin-bottom:20px">
                    Company:
                    <b>@SelectedOrders.FirstOrDefault()?.Customer?.CompanyName</b>
                </RadzenCard>
                <RadzenTabs>
                    <Tabs>
                        <RadzenTabsItem Text="Order Details">
                            <RadzenDataGrid AllowFiltering="true" AllowPaging="true" AllowSorting="true" Data="@(SelectedOrders.FirstOrDefault()?.OrderDetails)" TItem="OrderDetail">
                                <Columns>
                                    <RadzenDataGridColumn TItem="OrderDetail" Property="Order.CustomerID" Title="Order" />
                                    <RadzenDataGridColumn TItem="OrderDetail" Property="Product.ProductName" Title="Product" />
                                    <RadzenDataGridColumn TItem="OrderDetail" Property="UnitPrice" Title="Unit Price">
                                        <Template Context="detail">
                                            @String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", detail.UnitPrice)
                                        </Template>
                                    </RadzenDataGridColumn>
                                    <RadzenDataGridColumn TItem="OrderDetail" Property="Quantity" Title="Quantity" />
                                    <RadzenDataGridColumn TItem="OrderDetail" Property="Discount" Title="Discount">
                                        <Template Context="detail">
                                            @String.Format("{0}%", detail.Discount * 100)
                                        </Template>
                                    </RadzenDataGridColumn>
                                </Columns>
                            </RadzenDataGrid>
                        </RadzenTabsItem>
                        <RadzenTabsItem Text="Products">
                            <RadzenDataList WrapItems="true" AllowPaging="true" Data="@(SelectedOrders.FirstOrDefault()?.OrderDetails)" TItem="OrderDetail" PageSize="10">
                                <Template Context="detail">
                                    <RadzenCard Style="width:100px;height:100px">
                                        Product:
                                        <b>@detail?.Product?.ProductName</b>
                                    </RadzenCard>
                                </Template>
                            </RadzenDataList>
                        </RadzenTabsItem>
                    </Tabs>
                </RadzenTabs>
            </div>
        </div>
    </div>
}

@code {
    IList<Order> SelectedOrders { get; set; }

    IQueryable<Order> orders;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        orders = dbContext.Orders
            .Include("Customer")
            .Include("Employee")
            .Include("OrderDetails")
            .Include("OrderDetails.Product");

        SelectedOrders = new List<Order>(){ orders.FirstOrDefault() };
    }
}
